package com.gdth.bank.activity;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.greenrobot.eventbus.EventBus;

import com.gdth.bank.BankApplication;
import com.gdth.bank.R;
import com.gdth.bank.event.CjkhjlListEvent;
import com.gdth.bank.presenter.CJkhxxPresenter;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.view.CJkhxxView;
import com.gdth.bank.widget.BankDrawDownListTextView;
import com.gdth.bank.widget.BankEditText;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.database.bean.Cjkhjl;
import com.gdth.database.bean.TAppBfrjJbxxb;
import com.gdth.database.bean.TAppBfrjJtcyb;
import com.gdth.database.bean.TAppBfrjLxfs;
import com.gdth.database.bean.TAppYh;
import com.gdth.model.Item;
import com.gdth.utils.RegularUtil;
import com.gdth.utils.UUIDUtil;
import com.gdth.widget.dialog.BaseToast;
import com.gdth.widget.dialog.DialogConfig;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * 采集客户基本信息页面，
 * 每次采集新的客户，必须先跳到该页面添加基础信息，再添加拜访记录
 * @author arvin
 *
 */
public class CjkhxxActivity extends BaseActivity implements CJkhxxView{
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left, click="finishClick") TextView txt_header_left;
    @ViewInject(id=R.id.header_btn_right, click="save") TextView txt_header_right;
    
    @ViewInject(id=R.id.cjkhxx_xm) BankEditText txt_khxm;
	@ViewInject(id=R.id.cjkhxx_zjlb) BankDrawDownListTextView drawDownList_zjlx;
	@ViewInject(id=R.id.cjkhxx_zjhm) BankEditText txt_zjhm;
	@ViewInject(id=R.id.cjkhxx_lxfs) BankEditText txt_lxfs;
    
	List<Item> zjList = new ArrayList<Item>();
	CJkhxxPresenter mCJkhxxPresenter;
	TAppYh user;
	TAppBfrjJbxxb mTAppBfrjJbxxb;
	int type = 0;
	String oldUUid = "";
	String oldKhxm = "";
	String oldsfz = "";

	
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cjkhxx);
		
		initView();
	}
    
    private void initView() {
		type = getIntent().getIntExtra("type",0);
		if(type == 1){
			oldUUid = getIntent().getStringExtra("uuid");
			oldKhxm = getIntent().getStringExtra("khxm");
			oldsfz = getIntent().getStringExtra("sfz");
		}

		txt_header_title.setText("添加客户");
    	txt_khxm.setLabel(R.string.cjkh_khxx_khxm);
		txt_khxm.setHint(R.string.cjkh_khxx_khxm_hint);
		txt_khxm.setImage(null);
		
		drawDownList_zjlx.setLabel(R.string.cjkh_khxx_zjlb);
		txt_zjhm.setLabel(R.string.cjkh_khxx_zjhm);
		txt_zjhm.setHint(R.string.cjkh_khxx_zjhm_hint);
		txt_zjhm.setImage(null);
		
		/*勇哥说写死，感觉早晚会解封的，所以先不删除
		zjList = AppUtil.getCanshuByType(this, AppUtil.SHARE_DATA_ZJLX_CJKH);
		*/
		Item item = new Item();
		item.setKey("1");
		item.setValue(getResources().getString(R.string.param118));
		zjList.add(item);
		
		drawDownList_zjlx.setItem(zjList);
//		if (AppUtil.isApkDebugable(this)) {
//			txt_khxm.setValue("胡燕");
//			txt_zjhm.setValue("430223198503264246");
//		}
		
		drawDownList_zjlx.setValue("1");
		txt_lxfs.setLabel(getResources().getString(R.string.param119));
		txt_lxfs.setHint(getResources().getString(R.string.param120));
		txt_lxfs.setImage(null);
		txt_lxfs.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
		if(type == 1){
			txt_khxm.setValue(oldKhxm);
			txt_zjhm.setValue(oldsfz);
		}
		mCJkhxxPresenter = new CJkhxxPresenter(this);
		
		user = BankApplication.mInstance.getCurrentUser();
    }
    
    public void save(View arg0) {
    	String khxm = txt_khxm.getValue().toString();
		if (khxm == null || khxm.equals("")){
			txt_khxm.mEditText.requestFocus();
			showMessage(getResources().getString(R.string.param121));
			return;
		}
		
		Object zjlx = drawDownList_zjlx.getKey();
		if (zjlx == null || zjlx.equals("")){
			showMessage(getResources().getString(R.string.param122));
			return;
		}
		
		String zjhm = txt_zjhm.getValue().toString();
		if (zjhm == null || zjhm.equals("")){
			txt_zjhm.mEditText.requestFocus();
			showMessage(getResources().getString(R.string.param123));
			return;
		}
		try {
			if (zjlx.toString().equals("1")) {
				String isSfz = RegularUtil.IDCardValidate(zjhm);
				if (!isSfz.equals("")) {
					showMessage(isSfz);
					return;
				}
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			showMessage(getResources().getString(R.string.param124));
			return;
		}
		String lxfs = txt_lxfs.getValue().toString();

//		if(null == lxfs || lxfs.isEmpty()){
//			showMessage("联系电话不能为空");
//		}
		
		if (!lxfs.equals("")) {
			//boolean isMobile = RegularUtil.isMobile(lxfs);
			//boolean isPhone = RegularUtil.isPhone(lxfs);
			//if (!isMobile && !isPhone){
			//	BaseToast.getInstance().ShowSystemToast(this, "电话号码格式不正确");
			//	return;
			//}
			if(!RegularUtil.telCheck(lxfs)){
				BaseToast.getInstance().ShowSystemToast(this, getResources().getString(R.string.param125));
				return;
			}
		}
		String jcuuid = "";
		if(type == 1 ){
			jcuuid = oldUUid;
		}
		if(jcuuid.isEmpty() ||jcuuid.equals("") ){
			jcuuid = UUIDUtil.getUUID();
		}
		int zjlb = Integer.valueOf(zjlx.toString());
    	mTAppBfrjJbxxb = new TAppBfrjJbxxb();
    	mTAppBfrjJbxxb.setCjr(user.getXm());
    	mTAppBfrjJbxxb.setCjrsfz(user.getZh());
    	mTAppBfrjJbxxb.setCjsj(new Date());
    	mTAppBfrjJbxxb.setUuid(jcuuid);
    	mTAppBfrjJbxxb.setZjhm(zjhm);
    	mTAppBfrjJbxxb.setZjlb(zjlb);
    	mTAppBfrjJbxxb.setKhxm(khxm);
    	mTAppBfrjJbxxb.setBbh(1);
    	mTAppBfrjJbxxb.setSjzt(AppUtil.sjzt_create);
    	
    	//保存本人的家庭记录
    	String jtuuid = UUIDUtil.getUUID();
		TAppBfrjJtcyb mTAppBfrjJtcyb = new TAppBfrjJtcyb();
		mTAppBfrjJtcyb.setCjsj(new Date());
		mTAppBfrjJtcyb.setCyxm(khxm);
		mTAppBfrjJtcyb.setJcuuid(jcuuid);
		mTAppBfrjJtcyb.setJtgx(AppUtil.jtgx_huzhu);
		mTAppBfrjJtcyb.setBbh(1);
		mTAppBfrjJtcyb.setSjzt(AppUtil.sjzt_create);
		mTAppBfrjJtcyb.setUuid(jtuuid);
		mTAppBfrjJtcyb.setZjhm(zjhm);
		mTAppBfrjJtcyb.setZjlb(zjlb);
		mTAppBfrjJtcyb.setJtgx(7);
		mTAppBfrjJtcyb.setKhzy(1);
		
		List<TAppBfrjLxfs> lxfss = null;
		
		if (!lxfs.equals("")) {
			lxfss = new ArrayList<TAppBfrjLxfs>();
			TAppBfrjLxfs mTAppBfrjLxfs = new TAppBfrjLxfs();
			mTAppBfrjLxfs.setBbh(1);
			mTAppBfrjLxfs.setCjsj(new Date());
			mTAppBfrjLxfs.setCyxm(khxm);
			mTAppBfrjLxfs.setDhhm(lxfs);
			mTAppBfrjLxfs.setJcuuid(jcuuid);
			mTAppBfrjLxfs.setJtuuid(jtuuid);
			mTAppBfrjLxfs.setSjzt(AppUtil.sjzt_create);
			mTAppBfrjLxfs.setUuid(UUIDUtil.getUUID());
			
			lxfss.add(mTAppBfrjLxfs);
		}
		
		List<TAppBfrjJtcyb> jtcys = new ArrayList<TAppBfrjJtcyb>();
		jtcys.add(mTAppBfrjJtcyb);

		//mTAppBfrjJbxxb.__setDaoSession();
//		mTAppBfrjJbxxb.getJtcys().addAll(jtcys);
//		mTAppBfrjJbxxb.getLxfss().addAll(lxfss);
		if(type == 1 ){
			mCJkhxxPresenter.updataCjkhjbxx(mTAppBfrjJbxxb);
		}else{
			mTAppBfrjJbxxb.setJtcys(jtcys);
			mTAppBfrjJbxxb.setLxfss(lxfss);
			mCJkhxxPresenter.save(mTAppBfrjJbxxb);
		}


    }

	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		BaseToast.getInstance().ShowSystemToast(this, msg);
	}

	@Override
	public void updateView(Cjkhjl mCjkhjl) {
		// TODO Auto-generated method stub
		EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.refreshList));
		DialogConfig config = new DialogConfig(this,"修改完成" , true,false,DoexitClickListener);
		config.show();
	}

	@Override
	public void saveSuccess() {
		// TODO Auto-generated method stub
		EventBus.getDefault().post(new CjkhjlListEvent(CjkhjlListEvent.refreshList));
		onAddClick(getResources().getString(R.string.param126));
	}
	
	public void onAddClick(String tishi){
		DialogConfig config = new DialogConfig(this,tishi , true, exitClickListener);
		config.show();
	}
	
	private OnClickListener exitClickListener = new  OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(CjkhxxActivity.this, CjkhjlActivity.class);
			intent.putExtra("wybs", mTAppBfrjJbxxb.getUuid());
			intent.putExtra("type", 0);
			
			startActivity(intent);
			
			finish();
		}
	};

	private OnClickListener DoexitClickListener = new  OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			finish();
		}
	};
}
